#include "parse_scheme.h"
#include <fstream>
#include "common.h"
void parseOneStage(const QpcrStageInfo & stageInfoIn);
void parseOneStep(const QpcrStepInfo & stepInfoIn, int isLastStep, int totalSteps);
int g_stepNum = 1;
int g_loopCnt = 0;
std::ofstream fd_qpcr_scheme;
void parseQpcrScheme(const QpcrSchemeParam & paramIn)
{
    int stagesNum = paramIn.stages_size();
    int i = 0;
    int moduleId = (paramIn.qpcrmodule()==GPRPC::ModuleType::MT_QPCR1)?0:1;
    char qpcrSchemeFile[128];
    memset(qpcrSchemeFile,0,128);
    sprintf(qpcrSchemeFile,"%s%s%d%s",FILE_QPCR_SCHEME_PATH,"qpcr",moduleId,"_scheme.txt");
    fd_qpcr_scheme.open(qpcrSchemeFile,std::ios::out|std::ios::binary|std::ios::trunc);

    if(!fd_qpcr_scheme.is_open())
    {
        std::cout<<"open file:"<<qpcrSchemeFile<<" failed"<<std::endl;
        return;
    } 
    g_stepNum = 1;
    for(i=0;i<stagesNum;i++)
    {        
        parseOneStage(paramIn.stages(i));
    }


    fd_qpcr_scheme.close();

}

void parseOneStage(const QpcrStageInfo & stageInfoIn)
{
    int stageType = stageInfoIn.stagetype();
    int stepsNum = stageInfoIn.qpcrsteps_size();
    std::cout<<"steps number in stage:"<<stepsNum<<std::endl;
    if(stageType == GPRPC::QpcrStageType::STAGE_KEEP)
    {
        std::cout<<"keep stage"<<std::endl;
        parseOneStep(stageInfoIn.qpcrsteps(0), 0, 0);
    }
    else if(stageType == GPRPC::QpcrStageType::STAGE_CYCLE)
    {
        g_loopCnt = stageInfoIn.repeattimes();
        std::cout<<"repeat times:"<<g_loopCnt<<std::endl;
        for(int i=0;i<(stepsNum-1);i++)
        {
            parseOneStep(stageInfoIn.qpcrsteps(i), 0, 0); 
        }

        parseOneStep(stageInfoIn.qpcrsteps(stepsNum-1), 1, stepsNum); 

        g_loopCnt = 0;
       
    }
}

void parseOneStep(const QpcrStepInfo & stepInfoIn, int isLastStep, int numStepsInStage)
{
    char stepStr[256];
    int cycnt = 0;
    int loopcnt = isLastStep?(g_loopCnt-1):0;
    int loopstep = isLastStep?(numStepsInStage-1):0;
    int startstep = 1;
    float rate = ((float)stepInfoIn.tempchangev())/100;
    int keeptime = stepInfoIn.time()*10;
    int cycktimec = 0;
    float keeptem = stepInfoIn.temp();
    float cycktemc = 0.00;

    float loopktemc = 0.00;
    memset(stepStr,0,256);
    sprintf(stepStr,"%s%d& ","%step=",g_stepNum);
    sprintf(stepStr,"%s%s%d& ",stepStr,"cycnt=",cycnt);
    sprintf(stepStr,"%s%s%d& ",stepStr,"loopcnt=",loopcnt);
    sprintf(stepStr,"%s%s%d& ",stepStr,"loopstep=",loopstep);
    sprintf(stepStr,"%s%s%d& ",stepStr,"startstep=",startstep);
    sprintf(stepStr,"%s%s%.2f& ",stepStr,"rate=",rate);    
    sprintf(stepStr,"%s%s%03d& ",stepStr,"keeptime=",keeptime);
    sprintf(stepStr,"%s%s%d& ",stepStr,"cycktimec=",cycktimec);    
    sprintf(stepStr,"%s%s%05.1f& ",stepStr,"keeptem=",keeptem);
    if(cycktemc >= 0.00)
    {
        sprintf(stepStr,"%s%s%04.1f& ",stepStr,"cycktemc=+",cycktemc);
    }
    else
    {
        cycktemc = 0.00 - cycktemc;
        sprintf(stepStr,"%s%s%04.1f& ",stepStr,"cycktemc=-",cycktemc);        
    }

    if(loopktemc >= 0.00)
    {
        sprintf(stepStr,"%s%s%04.1f& ",stepStr,"loopktemc=+",loopktemc);
    }
    else
    {
        loopktemc = 0 - loopktemc;
        sprintf(stepStr,"%s%s%04.1f& ",stepStr,"loopktemc=-",loopktemc); 
    }

    int loopktimec = 0;
    sprintf(stepStr,"%s%s%d& ",stepStr,"loopktimec=",loopktimec);
    char needPhoto = 'y';
    std::cout<<"takepic:"<<stepInfoIn.bpic()<<std::endl;
    if(stepInfoIn.bpic())
    {
        needPhoto = 'y';
    }
    else
    {
        needPhoto = 'n';
    }
    sprintf(stepStr,"%s%s%c&\n",stepStr,"photo=",needPhoto); 

    //write string to file here;
    if(fd_qpcr_scheme.is_open())
    {
        fd_qpcr_scheme<<stepStr;
    }
    
    g_stepNum++;
}